package com.thysdy.flowable.controller;

import com.thysdy.flowable.entity.ActIdGroup;
import com.thysdy.flowable.enums.CodeEnum;
import com.thysdy.flowable.exception.MyException;
import com.thysdy.flowable.service.IActIdGroupService;
import com.thysdy.flowable.vo.ReturnVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ML
 * @since 2020-11-06
 */
@RestController
@RequestMapping("/act-id-group")
public class ActIdGroupController {
    @Resource
    IActIdGroupService groupService;

    @PostMapping("addGroup")
    public ReturnVo addGroup(@RequestBody ActIdGroup group) throws MyException {
        try {
            if (null != group.getName()) {
                groupService.save(group);
                return ReturnVo.success();
            } else {
                throw new MyException(CodeEnum.paramError);
            }
        } catch (MyException e) {
            throw e;
        } catch (Exception e) {
            return ReturnVo.exception(new MyException(CodeEnum.commonException));
        }

    }

    @PatchMapping("updateGroup")
    public ReturnVo updateGroup(@RequestBody ActIdGroup group) throws MyException {
        try {
            if (null != group.getId()) {
                groupService.updateById(group);
                return ReturnVo.success();
            } else {
                throw new MyException(CodeEnum.paramError);
            }
        } catch (MyException e) {
            throw e;
        } catch (Exception e) {
            return ReturnVo.exception(new MyException(CodeEnum.commonException));
        }

    }

    @DeleteMapping("batchDeleteGroup")
    public ReturnVo batchDeleteGroup(@RequestBody List<String> ids) throws MyException {
        try {
            if (null != ids) {
                groupService.removeByIds(ids);
                return ReturnVo.success();
            } else {
                throw new MyException(CodeEnum.paramError);
            }
        } catch (MyException e) {
            throw e;
        } catch (Exception e) {
            return ReturnVo.exception(new MyException(CodeEnum.commonException));
        }

    }

}

